<template>
  <div class="app-container">
    <el-form ref="classifyForm" :model="classify" label-width="120px" class="el-for-content">

      <el-form-item label="壁纸分类名称">
        <el-input v-model="classify.name" style="width: 10%"></el-input>
      </el-form-item>
      <el-form-item label="排序字段">
        <el-input placeholder="number" v-model.number="classify.sort" style="width: 7%"></el-input>
      </el-form-item>
      <el-form-item label="是否为推荐">
        <el-radio v-model="classify.recommend" label="1">是</el-radio>
        <el-radio v-model="classify.recommend" label="0">否</el-radio>
      </el-form-item>
      <el-form-item label="壁纸封面图上传">
        <el-upload
          :action="uploadFileUrl"
          :before-upload="handleBeforeUpload"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-success="handleUploadSuccess"
          :on-error="handleUploadError"
          :file-list="fileList"
          :limit="1"
        >
          <i class="el-icon-plus"></i>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png/jpeg文件，且不超过3MB</div>
        </el-upload>

        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>
      <el-form-item>
        <el-button v-show="publishVisible" type="primary" plain @click="publishClassify()">发布classify</el-button>
        <el-button v-show="editVisible" type="info" plain @click="editClassify()">修改classify</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import { Message } from 'element-ui'

import { addWallPaperClassify, editWallPaperClassify } from '@/api/classify'

export default {
  data() {
    return {
      classify: {
        name: '',
        picurl: '',
        sort: '',
        recommend:''
      },
      fileList:[],
      uploadFileUrl: 'http://localhost:8080/api/classify/upload',
      dialogImageUrl: '',
      dialogVisible: false,
      // 文件类型, 例如['png', 'jpg', 'jpeg']
      fileType: ['png', 'jpg', 'jpeg'],
      fileSize: 5,
      publishVisible: true,
      editVisible: false
    }
  },
  mounted() {
    if (this.$route.path === '/classify/publish/edit') {
      console.log(this.$route.query.classify)
      this.classify = this.$route.query.classify
      if (this.classify.recommend==="是"){
        this.classify.recommend="1"
      }else {
        this.classify.recommend="0"
      }
      //只展示修改按钮
      this.editVisible = true
      this.publishVisible = false
      //图片回显->使用数组最是稳健
      this.fileList.push({name:"classify",url:this.classify.picurl})
    }
  },
  methods: {
    demo(){
      alert(this.classify.select)
    },
    // 上传前校检格式和大小
    handleBeforeUpload(file) {
      // 校检文件类型
      if (this.fileType) {
        const fileName = file.name.split('.')
        const fileExt = fileName[fileName.length - 1]
        const isTypeOk = this.fileType.indexOf(fileExt) >= 0
        if (!isTypeOk) {
          Message.warning('文件格式不对')
          return false
        }
      }
      // 校检文件大小
      if (this.fileSize) {
        const isLt = file.size / 1024 / 1024 < this.fileSize
        if (!isLt) {
          Message(`上传文件大小不能超过 ${this.fileSize} MB!`)
          return false
        }
      }
      return true
    },
    // 上传失败
    handleUploadError(err) {
      console.log(err)
      Message.error('上传失败, 请重试！')
      this.classify = []
    },

    handleUploadSuccess(res, file) {
      console.log(res)
      if (res.code === 200) {
        console.log(res.data)
        this.classify.picurl = res.data
        console.log("上转后图片地址：",this.classify)
      } else {
        Message.error(res.message)
      }
    },

    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    publishClassify() {
      this.$refs.classifyForm.validate((valid) => {
        if (valid) {
          addWallPaperClassify(this.classify).then((res) => {
            Message.success(res.message)
            this.$router.push('/classify/list')
          }).catch(error=>{
            Message.error(error)
            this.$router.push("/classify/list")
          })
        } else {
          Message.warning('表格非法')
        }
      })
    },

    editClassify() {
         editWallPaperClassify(this.classify).then(res=>{
           Message.success("修改成功")
           this.$router.push("/classify/list")
         })
    }
  }
}
</script>

<style scoped>
.app-container {
  .el-for-content {
  }
}
</style>
